package com.trainoo.producer;

import com.trainoo.message.Message;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2020/6/28 10:21
 */
@Component
public class RocketMQProducer {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Scheduled(cron = "01 * * * * ?")
    public void sendMsg() throws InterruptedException {
        //发送同步消息
        Message<String> message = new Message<>();
        message.setId("123");
        message.setContent("for test, " + System.currentTimeMillis());
        rocketMQTemplate.asyncSend("broker-a", message, new SendCallback() {
            // 实现消息发送成功的后续处理
            @Override
            public void onSuccess(SendResult var1) {
                System.out.printf("async onSucess SendResult=%s %n", var1);
            }

            // 实现消息发送失败的后续处理
            @Override
            public void onException(Throwable var1) {
                System.out.printf("async onException Throwable=%s %n", var1);
            }
        });
        // 让主线程睡眠10秒
        Thread.sleep(10000);
    }
}
